const mqtt = require('aliyun-iot-mqtt');
//设备身份信息
var options = {
  productKey: "k0dhz9qJDMf",
  deviceName: "ds_01",
  deviceSecret: "8e5a0c406b41a12a1b662e9f588bafe0",
  host: "iot-06z00idh3gzja4u.mqtt.iothub.aliyuncs.com"
};

//建立MQTT连接
const client = mqtt.getAliyunIotMqttClient(options);
//订阅云端指令Topic
client.subscribe(`/${options.productKey}/${options.deviceName}/user/get`)
client.subscribe(`/sys/${options.productKey}/${options.deviceName}/thing/event/property/post_reply`)
client.on('message', function (topic, message) {
  console.log("topic " + topic)
  console.log("message " + message)
})

setInterval(function () {
  //定时上报数据
  client.publish(`/sys/${options.productKey}/${options.deviceName}/thing/event/property/post`, getPostData(), { qos: 0 });
}, 8 * 1000);


//音量 取值范围：0 ~ 100
var Volume = 100;

function getPostData() {
	
	
	
  const payloadJson = {
    id: Date.now(),
    version: "1.0",
    params: {
	  PowerSwitch: 1,
	  Channel: 2,
	  Volume: Volume--
    },
    method: "thing.event.property.post"

  }
  console.log("payloadJson " + JSON.stringify(payloadJson))
  return JSON.stringify(payloadJson);
}